package com.jerry.jul;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.*;

/**
 * Jul测试
 *
 * @author Jerry
 * @since 2024/11/16 10:26
 */
public class JulTest {

    @Test
    public void testQuick() {
        // 获取日志记录器
        Logger logger = Logger.getLogger("com.jerry.jul.JulTest");
        // 日志记录输出
        logger.info("Hello Jul");

        // 通用方法进行日志记录
        logger.log(Level.INFO, "info msg");

        // 通过占位符输出变量值
        String name = "Jerry";
        int age = 35;
        logger.log(Level.INFO, "用户信息：{0}, {1}", new Object[]{name, age});
    }

    @Test
    public void testLogLevel() {
        // 获取日志记录器
        Logger logger = Logger.getLogger("com.jerry.jul.JulTest");
        // 日志记录输出
        logger.log(Level.SEVERE, "SEVERE");
        logger.log(Level.WARNING, "WARNING");
        logger.log(Level.INFO, "INFO");
        logger.log(Level.CONFIG, "CONFIG");
        logger.log(Level.FINE, "FINE");
        logger.log(Level.FINER, "FINER");
        logger.log(Level.FINEST, "FINEST");
    }

    @Test
    public void testLogConfig() throws IOException {
        // 获取日志记录器
        Logger logger = Logger.getLogger("com.jerry.jul.JulTest");

        // 关闭系统默认配置
        logger.setUseParentHandlers(false);
        // 配置日志级别
        logger.setLevel(Level.ALL);

        // 配置控制台输出
        ConsoleHandler consoleHandler = new ConsoleHandler();
        // 格式转换对象
        SimpleFormatter formatter = new SimpleFormatter();
        consoleHandler.setFormatter(formatter);
        logger.addHandler(consoleHandler);
        consoleHandler.setLevel(Level.ALL);

        // 文件输出
        FileHandler fileHandler = new FileHandler("src/main/resources/jul.log");
        fileHandler.setFormatter(formatter);
        logger.addHandler(fileHandler);
        fileHandler.setLevel(Level.WARNING);

        // 日志记录输出
        logger.log(Level.SEVERE, "SEVERE");
        logger.log(Level.WARNING, "WARNING");
        logger.log(Level.INFO, "INFO");
        logger.log(Level.CONFIG, "CONFIG");
        logger.log(Level.FINE, "FINE");
        logger.log(Level.FINER, "FINER");
        logger.log(Level.FINEST, "FINEST");
    }

    @Test
    public void testLogParent() {
        // 获取日志记录器
        Logger logger1 = Logger.getLogger("com.jerry.jul");
        Logger logger2 = Logger.getLogger("com");

        System.out.println(logger1.getParent() == logger2);
        // 所有日志记录器的顶级父元素：LogManager$RootLogger，它的name是空字符串
        System.out.println("logger2 parent: " + logger2.getParent() + ", name" + logger2.getParent().getName());

        // 关闭系统默认配置
        logger2.setUseParentHandlers(false);

        // 配置控制台输出
        ConsoleHandler consoleHandler = new ConsoleHandler();
        // 格式转换对象
        SimpleFormatter formatter = new SimpleFormatter();
        consoleHandler.setFormatter(formatter);
        logger2.addHandler(consoleHandler);

        // 配置日志级别
        logger2.setLevel(Level.ALL);
        consoleHandler.setLevel(Level.ALL);

        // 日志记录输出
        logger1.log(Level.SEVERE, "SEVERE");
        logger1.log(Level.WARNING, "WARNING");
        logger1.log(Level.INFO, "INFO");
        logger1.log(Level.CONFIG, "CONFIG");
        logger1.log(Level.FINE, "FINE");
        logger1.log(Level.FINER, "FINER");
        logger1.log(Level.FINEST, "FINEST");
    }

    @Test
    public void testProperties() throws IOException {
        // 读取配置文件
        InputStream stream = JulTest.class.getClassLoader().getResourceAsStream("logging.properties");

        // 创建LogManager对象
        LogManager logManager = LogManager.getLogManager();

        // 通过LogManager对象加载文件
        logManager.readConfiguration(stream);

        Logger logger = Logger.getLogger("com.jerry.jul");
        logger.log(Level.SEVERE, "SEVERE 111");
        logger.log(Level.WARNING, "WARNING");
        logger.log(Level.INFO, "INFO");
        logger.log(Level.CONFIG, "CONFIG");
        logger.log(Level.FINE, "FINE");
        logger.log(Level.FINER, "FINER");
        logger.log(Level.FINEST, "FINEST");

        Logger logger2 = Logger.getLogger("test");
        logger2.log(Level.SEVERE, "SEVERE test");
        logger2.log(Level.WARNING, "WARNING test");
        logger2.log(Level.INFO, "INFO test");
        logger2.log(Level.CONFIG, "CONFIG test");
        logger2.log(Level.FINE, "FINE test");
        logger2.log(Level.FINER, "FINER test");
        logger2.log(Level.FINEST, "FINEST test");
    }
}
